/* Name: StarField
 * Author: John Gideon
 * Date: 5-8-12
 * Description: Draws either a star field in the background that can be moving or a static one that 
 *  responds to view movement.
 * Revisions: See Subversion logs at http://code.google.com/p/software-engineering-2012/source/list
 */

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Point;

public class StarField {
	
	final private static int STAR_SPEED = 10;
	private static float x[];
	private static float y[];
	private static float scale[];
	private static int nStars;
	private static Boolean isGame;
	
	// Sets the initial random placement of the stars and sets whether they should move or track the view
	public static void init(Boolean pIsGame) {
		isGame = pIsGame;
		float xMax, yMax;
		if (isGame) {
			xMax = (GameScreen.getWidth() / (float)STAR_SPEED);
			yMax = (GameScreen.getHeight() / (float)STAR_SPEED);
			nStars = 2000;
		} else {
			xMax = 0;
			yMax = 0;
			nStars = 500;
		}
		x = new float[nStars];
		y = new float[nStars];
		scale = new float[nStars];
		for (int i = 0; i < nStars; i++) {
			scale[i] = (float) (1.0f+Math.pow(Game.randomFloat(3.0f),3)/9.0f);
			x[i] = Game.randomInt(Game.screenWidth + (int)(scale[i]*xMax));
			y[i] = Game.randomInt(Game.screenHeight + (int)(scale[i]*yMax));
		}
	}
	
	// Called every frame: Updates and draws the position of the stars depending on the isGame variable
	public static void draw() {
		Graphics2D gh = Game.getGraphicsHandle();
		gh.setColor(Color.WHITE);
		if (isGame) {
			int xOffset = ViewControl.getX()/STAR_SPEED;
			int yOffset = ViewControl.getY()/STAR_SPEED;
			for (int i = 0; i < nStars; i++) {
				gh.fillOval((int)(x[i]-(xOffset*scale[i])), (int)(y[i]-(yOffset*scale[i])), 
						(int)Math.ceil(scale[i]), (int)Math.ceil(scale[i]));
			}
		} else {
			for (int i = 0; i < 
					nStars; i++) {
				y[i] += scale[i]*3.5;
				if (y[i] >= Game.screenHeight) {
					y[i] -= Game.screenHeight;
					x[i] = Game.randomInt(Game.screenWidth);
				}
				gh.fillOval((int)x[i], (int)y[i], (int)Math.ceil(scale[i]), (int)Math.ceil(scale[i]));
			}
		}
	}
	
}